package fpt.edu.vn.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import fpt.edu.vn.dao.AddnewDao;
import fpt.edu.vn.dao.TopicsDao;
import fpt.edu.vn.model.Districts;
import fpt.edu.vn.model.Provinces;
import fpt.edu.vn.model.Statustopics;
import fpt.edu.vn.model.Topics;
import fpt.edu.vn.model.Topictypes;

/* 
 * AdminListTopicDoAction.java 
 *  
 * 0.1
 * 
 * 10/15/2012 
 *  
 * Copyright notice 
 *  
 * Modification Logs: 
 *   DATE        AUTHOR      DESCRIPTION 
 *  -------------------------------------------------------- 
 *  15-Oct-2012  ThuongND    Create new 
 */
public class AdminListTopicDoAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private String email;

	private String Username;

	private List<Provinces> lstProvince;
	private List<Topictypes> lstTopictypes;
	private List<Object> lstTopics;
	// field get search
	private String search_field;
	private String search_author;
	private int city;
	private int district;
	private String fromnewsdate;
	private String tonewsdate;
	private int news_per_page;
	private int search_topic_type;
	private String search_order_d;
	private int search_topic_status;
	
	private String[] selected_news;
	private String massactions;
	
	private List<Districts> lstDistrict;
	private List<Statustopics> lstStatustopics;
	private AddnewDao addnewdao = new AddnewDao();
	private TopicsDao topicdao = new TopicsDao();
	// For SessionAware
	Map<String, Object> session;
	private boolean deleteTopic(String[] selected_news){
		topicdao.deletedMultiTopic(selected_news);
		return true;
	}
	private boolean showTopic(String[] selected_news){
		addnewdao.showMulti(selected_news);
		return true;
	}
	private boolean hideTopic(String[] selected_news){
			addnewdao.hideMulti(selected_news);
			return true;
		}

	public String doAction(){
		if(selected_news==null){
			return NONE;
		}
		session = ActionContext.getContext().getSession();
		if (session.containsKey("userRoleIdss")) {
			email = (String) session.get("emailss");
			int isAdmin = (Integer) session.get("userRoleIdss");
			if (isAdmin == 1 && selected_news!=null) {
				//do somthing with action
				if("".equals(massactions)){
					//unselect action don't do anything
				}else if("mass_hide_topic".equals(massactions)){
					hideTopic(selected_news);
				}else if("mass_show_topic".equals(massactions)){
					showTopic(selected_news);
				}else if("mass_delete".equals(massactions)){
					deleteTopic(selected_news);
				}
				
				// get district
				this.lstProvince = addnewdao.getProvince();
				this.lstTopictypes = topicdao.topictype();
				this.lstStatustopics = topicdao.topicstatus();
				this.lstTopics = topicdao.listalltopicSearch(search_field,
						search_author, city, district, fromnewsdate,
						tonewsdate, search_topic_type, search_order_d,search_topic_status);
				if(city!=0){
					this.lstDistrict = addnewdao.getDistricts(city);
				}
				if(news_per_page==0){
					news_per_page =20;
				}
				return SUCCESS;
			}
		}
		return ERROR;
	}
	public String execute() {
		session = ActionContext.getContext().getSession();
		if (session.containsKey("userRoleIdss")) {
			email = (String) session.get("emailss");
			int isAdmin = (Integer) session.get("userRoleIdss");
			if (isAdmin == 1) {
				// get district
				this.lstProvince = addnewdao.getProvince();
				this.lstTopictypes = topicdao.topictype();
				this.lstStatustopics = topicdao.topicstatus();
				this.lstTopics = topicdao.listalltopicSearch(search_field,
						search_author, city, district, fromnewsdate,
						tonewsdate, search_topic_type, search_order_d,search_topic_status);
				if(city!=0){
					this.lstDistrict = addnewdao.getDistricts(city);
				}
				if(news_per_page==0){
					news_per_page =20;
				}
				return SUCCESS;
			}
		}
		return ERROR;

	}

	public Date getSysdate() {
		Date date = new Date();
		return date;
	}

	public String getStrDate() {
		String curdate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(Calendar.getInstance().getTime());
		return curdate;
	}
	
	public String[] getSelected_news() {
		return selected_news;
	}
	public void setSelected_news(String[] selected_news) {
		this.selected_news = selected_news;
	}
	public String getMassactions() {
		return massactions;
	}
	public void setMassactions(String massactions) {
		this.massactions = massactions;
	}
	public List<Statustopics> getLstStatustopics() {
		return lstStatustopics;
	}

	public void setLstStatustopics(List<Statustopics> lstStatustopics) {
		this.lstStatustopics = lstStatustopics;
	}

	public int getSearch_topic_status() {
		return search_topic_status;
	}

	public void setSearch_topic_status(int search_topic_status) {
		this.search_topic_status = search_topic_status;
	}

	public List<Districts> getLstDistrict() {
		return lstDistrict;
	}

	public void setLstDistrict(List<Districts> lstDistrict) {
		this.lstDistrict = lstDistrict;
	}

	public String getSearch_field() {
		return search_field;
	}

	public void setSearch_field(String search_field) {
		this.search_field = search_field;
	}

	public String getSearch_author() {
		return search_author;
	}

	public void setSearch_author(String search_author) {
		this.search_author = search_author;
	}

	public int getCity() {
		return city;
	}

	public void setCity(int city) {
		this.city = city;
	}

	public int getDistrict() {
		return district;
	}

	public void setDistrict(int district) {
		this.district = district;
	}

	public String getFromnewsdate() {
		return fromnewsdate;
	}

	public void setFromnewsdate(String fromnewsdate) {
		this.fromnewsdate = fromnewsdate;
	}

	public String getTonewsdate() {
		return tonewsdate;
	}

	public void setTonewsdate(String tonewsdate) {
		this.tonewsdate = tonewsdate;
	}

	public int getNews_per_page() {
		return news_per_page;
	}

	public void setNews_per_page(int news_per_page) {
		this.news_per_page = news_per_page;
	}

	public int getSearch_topic_type() {
		return search_topic_type;
	}

	public void setSearch_topic_type(int search_topic_type) {
		this.search_topic_type = search_topic_type;
	}

	public String getSearch_order_d() {
		return search_order_d;
	}

	public void setSearch_order_d(String search_order_d) {
		this.search_order_d = search_order_d;
	}

	public List<Topictypes> getLstTopictypes() {
		return lstTopictypes;
	}

	public void setLstTopictypes(List<Topictypes> lstTopictypes) {
		this.lstTopictypes = lstTopictypes;
	}

	public List<Object> getLstTopics() {
		return lstTopics;
	}

	public void setLstTopics(List<Object> lstTopics) {
		this.lstTopics = lstTopics;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getUsername() {
		return Username;
	}

	public void setUsername(String username) {
		Username = username;
	}

	public List<Provinces> getLstProvince() {
		return lstProvince;
	}

	public void setLstProvince(List<Provinces> lstProvince) {
		this.lstProvince = lstProvince;
	}
}
